<?php require('common/isLogin.php');?>
<?php require('common/mysql.php');?>
<?php 
	$sqlObj = new Sql();  
	$username = $_COOKIE['user'];
	$allzd = $sqlObj->getAll("SELECT * FROM `account` where byuser = '{$username}'");
    $userinfo = $sqlObj->get_row("SELECT * FROM `user` where username = '{$username}'");
	$zctotalMoney = 0;  
	$srtotalMoney = 0;  
	// 遍历数组  
	foreach ($allzd as $item) {  
	    // 假设money字段总是存在并且是数值型字符串  
	    // 将字符串转换为浮点数并累加到totalMoney中  
		if($item['zcsr']==0){		//0为支出
			$zctotalMoney += (float)$item['money'];  
		}else{
			$srtotalMoney += (float)$item['money'];  
		}
	    
	}  
	//饼图计算
	$usecate = array();
	for($i=0;$i<count($allzd);$i++){
		$usecate[$i]["cate"]=$allzd[$i]["cate"];
		$usecate[$i]["sum"]=0;
	}
	// 创建一个空数组来存储唯一的分类
	$uniqueusecate = array();
	
	// 使用 for 循环遍历原始数组
	for ($i = 0; $i < count($usecate); $i++) {
	    // 获取当前分类
	    $currentCate = $usecate[$i]['cate'];
	    
	    // 检查当前分类是否已在新数组中
	    $found = false;
	    foreach ($uniqueusecate as $uniqueCate) {
	        if ($uniqueCate['cate'] === $currentCate) {
	            $found = true;
	            break;
	        }
	    }
	    
	    // 如果当前分类不在新数组中，则添加它
	    if (!$found) {
	        $uniqueusecate[] = $usecate[$i];
	    }
	}

	for($i=0;$i<count($allzd);$i++){
		$nowKey = getKey($allzd[$i]['cate'],$uniqueusecate);
		$uniqueusecate[$nowKey]["sum"] += $allzd[$i]['money'];
	}
	//var_dump(getKey('其他',$uniqueusecate)); 
	//var_dump($uniqueusecate); 	最终统计
	function getKey($soname, $array) {
	    foreach ($array as $key => $value) {
	        if ($value['cate'] == $soname) {
	            return $key; // 返回找到的键
	        }
	    }
	    return false; // 如果没有找到，返回 false
	}
  
	$sqlObj->close(); // 关闭数据库连接 
?>  
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>Hadi.个人中心</title>
		<link rel="stylesheet" href="css/style.css" />
		<link rel="stylesheet" href="css/bootstrap.min.css" />
	</head>
	<body>
		<!--头部-->
		<div class="container-fluid header">
		  <div class="row hd_title">
			  欢迎你，<?php echo $_COOKIE['user'];?>
		  </div>
		  <div class="row hd_date">
			  <?php echo date('Y-m');?>
		  </div>
		</div>
		<!--白色计算信息-->
		<div class="container money" style="padding: 9px 20px;">
		  <div class="row justify-content-center">
		    <div class="col">
		      <span class="symbol">￥</span><span class="expM"><?php echo number_format($zctotalMoney,2);?></span>
		    </div>
		  </div>
		  <div class="row justify-content-center">
		    <div class="col">
		      <span class="expMy">月支出</span>
		    </div>
		  </div>
		  <div class="row justify-content-center moneyjs text-center">
		    <div class="col">
				<div class="row"><?php echo number_format($srtotalMoney,2);?></div>
				<div class="row mjsf">月收入</div>
		    </div>
		    <div class="col">
				<div class="row"><?php echo number_format($srtotalMoney-$zctotalMoney,2);?></div>
				<div class="row mjsf">月结余</div>
		    </div>
			<?php if(!$userinfo['budget']){?>
				<div class="col" onclick="window.location.href='editbudget.php'">
					<div class="row justify-content-center">预算金额</div>
					<div class="row justify-content-center mjsf">点击设置</div>
				</div>
			<?php }else{?>
				<div class="col" onclick="window.location.href='editbudget.php'">
					<div class="row justify-content-center"><?php echo number_format($userinfo['budget'],2);?></div>
					<div class="row justify-content-center mjsf">当前预算</div>
				</div>
			<?php }?>
		    
		  </div>
		</div>
		<!--白色计算信息-->
		<div class="container money" style="padding: 9px 20px;margin-top:10px;height:430px;">
		<div class="row justify-content-center" style="padding:3px;">
			<div class="col" style="font-size:23px;">
				总 消 费
			</div>
		</div>
		<div class="row justify-content-center" style="padding:3px;">
			<div class="col">
				<canvas id="myChart" width="200" height="200"></canvas>
			</div>
		</div>
		  <?php for($i=0;$i<count($uniqueusecate);$i++){?>
			  <div class="row justify-content-center" style="padding:3px;">
					<div class="col" style="font-size:16px;">
						<?php echo $uniqueusecate[$i]["cate"];?>：<?php echo number_format($uniqueusecate[$i]["sum"],2);?> 元
					</div>
			  </div>
		  <?php }?>
		  
		</div>
		
		<!--记账按钮-->
		<div class="container text-center">
			<div class="row justify-content-center">
				<div class="col">
					<button class="jzbtn" onclick="javascript:window.location.href='account.php';"><img src="img/jzbtn.png" style="width: 28px;"/>&nbsp;记一笔账</button>
				</div>
			</div>
		</div>
		<!--更多账单-->
		<div class="container text-center">
			<div class="row justify-content-center">
				<div class="col">
					<button class="gdbtn" onclick="window.location.href='editpwd.php'"><img src="img/edit.png" style="width: 28px;"/>&nbsp;修改密码</button>
				</div>
			</div>
		</div>
		<?php include('footer.php');?>
	</body>
	<script src="js/chart.js"></script>
	<script>
	const ctx = document.getElementById('myChart');
	const data = {
	  labels: [
	    <?php for($i=0;$i<count($uniqueusecate);$i++){?>
			<?php echo '\''.$uniqueusecate[$i]["cate"].'\',';?>				
	    <?php }?>
	  ],
	  datasets: [{
	    label: '消费饼图',
	    //data: [300, 50, 100],
		data: [
	    <?php for($i=0;$i<count($uniqueusecate);$i++){?>
			<?php echo $uniqueusecate[$i]["sum"].',';?>				
	    <?php }?>],
	    backgroundColor: [
	      'rgb(255, 99, 132)',
	      'rgb(54, 162, 235)',
	      'rgb(255, 205, 86)',
	      'rgb(50, 182, 205)',
	      'rgb(215, 105, 76)'
	    ],
	    hoverOffset: 4
	  }]
	};
	const config = {
	  type: 'pie',
	  data: data,
	  options: {
	    responsive: true, // 设置图表为响应式，根据屏幕窗口变化而变化
	    maintainAspectRatio: false,// 保持图表原有比例
	    scales: {
	      yAxes: [{
	        ticks: {
	          beginAtZero:true
			}
		  }]
	    }
	  }
	};
	const myChart = new Chart(ctx, config);
	</script>
</html>
